package com.gmail.ceac13;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class FotosCameraActivity extends Activity implements SurfaceHolder.Callback {
	
	 SurfaceView mSurfaceView ;
	 SurfaceHolder  mSurfaceHolder;
	 boolean mPreviewRunning;
	 Camera camera;
	 Bitmap bitmap = null; 
	 Button botao;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.camera);
        botao =(Button)findViewById(R.id.botao_camera);
        mSurfaceView = (SurfaceView) findViewById(R.id.camera);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        botao.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					camera.takePicture(shutterCallback, rawCallback, jpegCallback);
				}
        });
	}

	
	
	ShutterCallback shutterCallback = new ShutterCallback() {
		  public void onShutter() {
			  Log.d("fotoNovo", "onShutter'd");
		  }
		};
		
		PictureCallback rawCallback = new PictureCallback() {
		  public void onPictureTaken(byte[] _data, Camera _camera) {
			  Log.d("fotoNovo", "onPictureTaken - raw");
		  }
		};
		
		PictureCallback jpegCallback = new PictureCallback() {
		  public void onPictureTaken(byte[] _data, Camera _camera) {
			  FileOutputStream outStream = null;
				try {
					outStream = new FileOutputStream(String.format(
							"/sdcard/%d.jpg", System.currentTimeMillis()));
					outStream.write(_data);
					outStream.close();
					Log.d("novoFo", "onPictureTaken - wrote bytes: " + _data.length);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
				}
				Log.d("fotoNovo", "onPictureTaken - jpeg");
			  
		  }
		};


	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (mPreviewRunning) {
			   camera.stopPreview();
		}
		   Camera.Parameters p = camera.getParameters();
		   p.setPreviewSize(width, height);
		   camera.setParameters(p);
	   try {
		   camera.setPreviewDisplay(holder);
	   } catch (IOException e) {
		   e.printStackTrace();
	   }
	   camera.startPreview();
	   mPreviewRunning = true;

		
	}


	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		camera = Camera.open();
		Camera.Parameters parametros = camera.getParameters();
  	  	parametros.setPictureFormat(PixelFormat.JPEG);
  	  	camera.setParameters(parametros);
	}


	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		  camera.stopPreview();
		  mPreviewRunning = false;
		  camera.release();
	}
}
